package edu.ccut.saturn.studio.logic.model.xml.loader;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnLogicNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnParameter;

public abstract class LogicNodeLoader extends NodeLoader {

	protected SaturnLogicNode createLogicNode() {
		return new SaturnLogicNode();
	}

	public List<Shape> loadNodes(Element root) {

		NodeList logicNodes = root
				.getElementsByTagName(SaturnLogicNode.KEY_NODE_LOGIC);

		List<Shape> shapes = new ArrayList<Shape>(logicNodes.getLength());

		for (int i = 0; i < logicNodes.getLength(); ++i) {
			SaturnLogicNode saturnLogicNode = createLogicNode();

			Node logicNode = logicNodes.item(i);
			saturnLogicNode.setNode(logicNode);
			String shapeId = super.getNodeAttributeValue(logicNode,
					Shape.KEY_ATTRIBUTE_SHAPE_ID);

			String nextId = super.getNodeAttributeValue(logicNode,
					Shape.KEY_ATTRIBUTE_SHAPE_NEXT_ID);

			String exception = super.getNodeAttributeValue(logicNode,
					SaturnLogicNode.SHOW_LOGIC_NODE_EXCEPTION);

			String key = super.getNodeElementValue(logicNode,
					SaturnLogicNode.SHOW_LOGIC_NODE_KEY);

			String name = super.getNodeElementValue(logicNode,
					SaturnLogicNode.SHOW_LOGIC_NODE_NAME);

			String transaction = this.getTransactionTypeValue(root, saturnLogicNode);

			String description = super.getNodeElementValue(logicNode,
					SaturnLogicNode.SHOW_LOGIC_NODE_DESCRITPION);

			List<SaturnParameter> parameters = super.getParameters(logicNode);

			Point location = super.getLocation(logicNode);
			Dimension size = new Dimension(50, 70);

			saturnLogicNode.setId(shapeId);
			saturnLogicNode.setLocation(location);
			saturnLogicNode.setKey(key);
			saturnLogicNode.setName(name);
			saturnLogicNode.setTransactionType(transaction);
			saturnLogicNode.setNextId(nextId);
			saturnLogicNode.setException(exception);
			saturnLogicNode.setDescription(description);
			saturnLogicNode.setParameters(parameters);
			saturnLogicNode.setSize(size);
			
			shapes.add(saturnLogicNode);
		}

		return shapes;
	}

	/**
	 * 载入Logic的transactionType
	 * 
	 */
	private String getTransactionTypeValue(Element element, SaturnLogicNode saturnLogicNode) {

		Node node=saturnLogicNode.getNode();
		
		Node tTypeNode = super.getChildNode(node,
				SaturnLogicNode.SHOW_LOGIC_NODE_TRANSACTION);

		String tTypeTemp = SelectionCandidate.transactionTypesForLogic[0];
		try {
			if (tTypeNode == null) {
				return tTypeTemp;
			}
			tTypeTemp = super.getNodeAttributeValue(tTypeNode,
					SaturnLogicNode.SHOW_LOGIC_NODE_TRANSACTIONTYPE);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return tTypeTemp;
	}
}
